package solutions.ferpa.ztest.intf;

import br.com.telescope.adsrt.dom.DomId;
import br.com.telescope.adsrt.dom.DomOpcao;
import br.com.telescope.adsrt.dom.DomText;
import br.com.telescope.adsrt.dom.DomTextLong;
import br.com.telescope.adsrt.lib.AdsException;
import br.com.telescope.adsrt.lib.Field;
import br.com.telescope.adsrt.lib.Form;
import br.com.telescope.sql.ModelQuery;
import br.com.telescope.t2.ServiceModel;
import br.com.telescope.t2.feature.DeleteFeature;
import br.com.telescope.t2.feature.InsertFeature;
import br.com.telescope.t2.feature.ListFeature;
import br.com.telescope.t2.feature.LookupFeature;
import br.com.telescope.t2.feature.ViewFeature;
import br.com.telescope.util.ObjectUtil;
import java.util.ArrayList;
import java.util.List;
import solutions.ferpa.ztest.ent.*;

/**
 * Cadastro de funções de uma pessoa.
 * <p>
 * Permite consultar/atualizar as funções que um usuário exerce no 
 * sistema.
 * <P>
 * Telescope origin: RTAUTH.CAD_FUNCOES_USUARIOS
 * <P>
 * Base entity: RTAUTH.FUNCOES_USUARIOS
 * <P>
 * @author TELESCOPE - Build task T2 Web Interface Generator
 * <P>
 * @version 0.1
 */
public class CadFuncoesUsuarios extends ServiceModel {
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    public static final String GLOBAL_NAME = "RTAUTH.CAD_FUNCOES_USUARIOS";
    
    @Override
    public String globalName() {
        return GLOBAL_NAME;
    }
    
    {
        setTitle("Cadastro de funções de uma pessoa");
    }
    
    protected EntFuncoesUsuarios fupe;
    protected EntFuncoesSistemas func;
    protected EntFuncoesUsuarios rec; // NEED_TJW1_COMPATIBILITY
    
    // Service parameters
    private DomId pUsuarioId;
    
    // Record items
    private DomId aFuncaoId;
    private DomId aUsuarioId;
    private DomOpcao aFuncao;
    private DomTextLong aDescricao;
    private DomText aTitulo;
    private DomId aId;
    
    // Query items
    
    @Override
    public String getBaseSql() {
        return "select FUPE.*"
                + " , FUNC.FUNCAO_SISTEMA as FUNCAO"
                + " , FUNC.DESCRICAO as DESCRICAO"
                + " , FUNC.TITULO as TITULO"
                + " , FUNC.ID as Z_FUNC_ID"
                + " from FUNCOES_USUARIOS FUPE"
                + " join FUNCOES_SISTEMAS FUNC on FUNC.ID = FUPE.FUNCAO_ID";
    }
    
    @Override
    public void initAttributes() {
        
        aFuncaoId = new DomId();
        aFuncaoId.context(ctx).name("FUNCAO_ID").label("Função");
        
        aUsuarioId = new DomId();
        aUsuarioId.context(ctx).name("USUARIO_ID").label("Usuário");
        
        aFuncao = new DomOpcao();
        aFuncao.context(ctx).name("FUNCAO").label("Função").group(true);
        
        aFuncao.setSqlOptions("SELECT FUNCAO_SISTEMA, FUNCAO_SISTEMA FROM FUNCOES_SISTEMAS FUNC ORDER BY 1");
        
        aDescricao = new DomTextLong();
        aDescricao.context(ctx).name("DESCRICAO").label("Descrição");
        aDescricao.setListOfValuesQuery(createQuery()
                .select("*")
                .from("(SELECT DESCRICAO as valor"
                + " , DESCRICAO as pesquisar"
                + " , DESCRICAO as descricao"
                + " from FUNCOES_SISTEMAS FUNC)", "q"));
        aDescricao.setAutocompleteLength(1);
        
        aTitulo = new DomText();
        aTitulo.context(ctx).name("TITULO").label("Título");
        aTitulo.setListOfValuesQuery(createQuery()
                .select("*")
                .from("(SELECT TITULO as valor"
                + " , TITULO as pesquisar"
                + " , TITULO as descricao"
                + " from FUNCOES_SISTEMAS FUNC)", "q"));
        aTitulo.setAutocompleteLength(1);
        
        aId = new DomId();
        aId.context(ctx).name("ID").label("Id");
        
    }
    
    @Override
    public void initParameters() {
        pUsuarioId = new DomId(ctx, "USUARIO_ID", "Função");
        pUsuarioId.setValue(ctx.getParameter("P_USUARIO_ID"));
        if (pUsuarioId.isNotNull()) {
            aUsuarioId.setValue(pUsuarioId.getValue());
        }
    }
    
    @Override
    public List<Field> getParameters() {
        if (allParameters == null) {
            allParameters = new ArrayList<Field>();
            allParameters.add(pUsuarioId);
        }
        return allParameters;
    }
    
    @Override
    public void copyBindParameters() {
        EntFuncoesUsuarios record = getRecord();
        if (pUsuarioId.isNotNull()) {
            record.setUsuarioId(pUsuarioId.getValue());
        }
    }
    
    @Override
    public void retrieveBindParameters() {
        pUsuarioId.setValue(getRecord().valueOfUsuarioId());
    }
    
    @Override
    public String getQueryPkColumn() {
        return "FUPE.ID";
    }
    
    @Override
    public void setId(String value) {
        this.aId.setValue(value);
    }
    
    @Override
    public EntFuncoesUsuarios getRecord() {
        if (fupe == null) {
            fupe = new EntFuncoesUsuarios(ctx);
            rec = fupe;
        }
        return this.fupe;
    }
    
    @Override
    public String getContextName() {
        return aFuncao.toText();
    }
    
    @Override
    public String getDefaultOrderBy() {
        return "FUNCAO, TITULO, DESCRICAO";
    }
    
    @Override
    public void translateInsertFks(Form form) {
        ModelQuery query;
        
        // FUNCAO_ID
        try {
            if (form.hasFields("FUNCAO")) {
                query = createQuery()
                        .select("FUNC.ID")
                        .from("FUNCOES_SISTEMAS", "FUNC")
                        .filter("FUNC.FUNCAO_SISTEMA", form.get("FUNCAO"))
                        ;
                fupe.setFuncaoId(ObjectUtil.toLong(translateFk(query)));
            }
        } catch (AdsException ex) {
            throw new RuntimeException(ex);
        }
    }
    
    @Override
    public void translateUpdateFks(Form form) {
        ModelQuery query;
        
        // FUNCAO_ID
        try {
            if (form.hasFields("FUNCAO", "DESCRICAO", "TITULO")) {
                query = createQuery()
                        .select("FUNC.ID")
                        .from("FUNCOES_SISTEMAS", "FUNC")
                        .filter("FUNC.FUNCAO_SISTEMA", form.get("FUNCAO"))
                        .filter("FUNC.DESCRICAO", form.get("DESCRICAO"))
                        .filter("FUNC.TITULO", form.get("TITULO"))
                        ;
                fupe.setFuncaoId(ObjectUtil.toLong(translateFk(query)));
            }
        } catch (AdsException ex) {
            throw new RuntimeException(ex);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="LIST">
    public class FList extends ListFeature {
        
        @Override
        public void initForm() {
            aFuncao.setOptional(false);
            addField(aFuncao, "view");
            addField(aTitulo, "view");
            addField(aDescricao, "view");
        }
        
        {
            setFeatureName("LIST");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="INSERT">
    public class FInsert extends InsertFeature {
        
        @Override
        public void initForm() {
            aFuncao.setOptional(false);
            addField(aFuncao, "rqd");
        }
        
        {
            setFeatureName("INSERT");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="DELETE">
    public class FDelete extends DeleteFeature {
        
        @Override
        public void initForm() {
            addField(aFuncao, "view");
        }
        
        {
            setFeatureName("DELETE");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="VIEW">
    public class FView extends ViewFeature {
        
        @Override
        public void initForm() {
            addField(aFuncao, "view");
            addField(aTitulo, "view");
            addField(aDescricao, "view");
        }
        
        {
            setFeatureName("VIEW");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="FUNCAO_LOOKUP">
    public class FFuncaoLookup extends LookupFeature {
        
        @Override
        public void initForm() {
            aFuncao.setEditable(true);
            addField(aFuncao, "opt");
            aDescricao.setEditable(false);
            addField(aDescricao, "view");
            aTitulo.setEditable(false);
            addField(aTitulo, "view");
        }
        
        @Override
        public ModelQuery getLookupQuery() {
            return createQuery()
                    .select("FUNC.FUNCAO_SISTEMA as FUNCAO")
                    .select("FUNC.DESCRICAO as DESCRICAO")
                    .select("FUNC.TITULO as TITULO")
                    .from("FUNCOES_SISTEMAS", "FUNC")
                    ;
        }
        
        {
            setFeatureName("FUNCAO_LOOKUP");
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="initFeatures">
    
    @Override
    protected void initFeatures() {
        addFeature(new FList());
        addFeature(new FInsert());
        addFeature(new FDelete());
        addFeature(new FView());
        addFeature(new FFuncaoLookup());
        super.initFeatures();
    }
    
    @Override
    public String getDefaultFeature() {
        return "LIST";
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="Getters and Setters">
    
    public Long getFuncaoId() {
        return aFuncaoId.getValue();
    }
    
    public void setFuncaoId(Long value) {
        this.aFuncaoId.setValue(value);
    }
    
    public Long getUsuarioId() {
        return aUsuarioId.getValue();
    }
    
    public void setUsuarioId(Long value) {
        this.aUsuarioId.setValue(value);
    }
    
    public String getFuncao() {
        return aFuncao.getValue();
    }
    
    public void setFuncao(String value) {
        this.aFuncao.setValue(value);
    }
    
    public String getDescricao() {
        return aDescricao.getValue();
    }
    
    public void setDescricao(String value) {
        this.aDescricao.setValue(value);
    }
    
    public String getTitulo() {
        return aTitulo.getValue();
    }
    
    public void setTitulo(String value) {
        this.aTitulo.setValue(value);
    }
    
    @Override
    public Long getId() {
        return aId.getValue();
    }
    
    public void setId(Long value) {
        this.aId.setValue(value);
    }
    
    public Long getParameterUsuarioId() {
        return pUsuarioId.getValue();
    }
    
    public void setParameterUsuarioId(Long value) {
        pUsuarioId.setValue(value);
        aUsuarioId.setValue(value);
    }
    // </editor-fold>
    
    
    @Override
    public String getJsServiceFile() {
        return "ui/rtauth/cad_funcoes_usuarios.js";
    }
}
